package com.zsc.newbuds.service.impl;

import com.zsc.newbuds.entity.Admin;
import com.zsc.newbuds.entity.ExchangeRecord;
import com.zsc.newbuds.entity.Shop;
import com.zsc.newbuds.mapper.ExchangeRecordMapper;
import com.zsc.newbuds.service.ExchangeRecordService;
import com.zsc.newbuds.utils.RedisUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigInteger;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 * @author D
 */
@Service
public class ExchangeRecordServiceImpl implements ExchangeRecordService {

    @Autowired
    ExchangeRecordMapper exchangeRecordMapper;

    @Autowired
    RedisUtil redisUtil;

    @Override
    public List<ExchangeRecord> getExchangeRecordList() {

        List<ExchangeRecord> exchangeRecords = new ArrayList<>();
        String key = "list:exchangeRecordList";
        if(redisUtil.hasKey(key)){
            System.out.println("从redis中获取list:exchangeRecordList");
            Map<Object, Object> map = redisUtil.hmget(key);
            for(Map.Entry<Object, Object> exchange:map.entrySet()){
                exchangeRecords.add((ExchangeRecord) exchange.getValue());
            }
        }else {
            System.out.println("从数据库中获取list:exchangeRecordList");
            Map< String,Object> map = new HashMap<>();
            exchangeRecords = exchangeRecordMapper.getExchangeRecordList();
            for (ExchangeRecord exchangeRecord : exchangeRecords){
                map.put(exchangeRecord.getExchangeId().toString(),exchangeRecord);
            }
            redisUtil.hmset(key,map);
        }
        return exchangeRecords;
    }

    @Override
    public List<ExchangeRecord> getUserExchangeRecordList(Integer userId) {
        List<ExchangeRecord> list = new ArrayList<>();
        String key= "list:exchangeRecordList";
        if(redisUtil.hasKey(key)){
            System.out.println("从redis中获取exchangeRecordList");
            Map<Object, Object> map = redisUtil.hmget(key);
            for(Map.Entry<Object, Object> exRecord:map.entrySet()){
                ExchangeRecord record = (ExchangeRecord) exRecord.getValue();
                if (record.getUserId().equals(userId)) {
                    System.out.println(record);
                    list.add(record);
                }
            }
        }else {
            System.out.println("从数据库中获取exchangeRecordList");
            list = exchangeRecordMapper.getUserExchangeRecordList(userId);
        }
        return list;
    }

    @Override
    public void addExchangeRecord(Integer goodsId, Integer userId) {
        String exchangeKey = "list:exchangeRecordList";
        String shopKey = "list:shopList";
        Shop shop = (Shop)redisUtil.hget(shopKey,goodsId.toString());
        ExchangeRecord exchangeRecord = new ExchangeRecord();
        exchangeRecord.setGoodsId(goodsId);
        exchangeRecord.setGoodsName(shop.getGoodsName());
        exchangeRecord.setGoodsPrice(shop.getGoodsPrice());
        exchangeRecord.setUserId(userId);
        exchangeRecord.setGoodsImage(shop.getGoodsImage());
        exchangeRecordMapper.addExchangeRecord(exchangeRecord);
        redisUtil.hset(exchangeKey,exchangeRecord.getExchangeId().toString(),exchangeRecord);
    }

    @Override
    public void provideNo(Integer[] exchangeId) {
        String key = "list:exchangeRecordList";
        for (Integer id:exchangeId){
            String no = getOrderNo().substring(0,16);
            ExchangeRecord exchangeRecord = (ExchangeRecord) redisUtil.hget(key,id.toString());
            exchangeRecord.setTrackingNumber(no);
            redisUtil.hset(key,id.toString(),exchangeRecord);
            exchangeRecordMapper.updateNo(id,no);
        }
    }

    /**
     * 生成随机数
     * @return
     */
    public String getOrderNo(){
        String orderNo = "" ;
        String trandNo = String.valueOf((Math.random() * 9 + 1) * 1000000);
        String sdf = new SimpleDateFormat("yyyyMMddHHMMSS").format(new Date());
        orderNo = trandNo.toString().substring(0, 4);
        orderNo = orderNo + sdf ;
        return orderNo ;
    }


}
